package com.hulushuju.core.net.http.okhttp;

import com.hulushuju.core.net.http.HttpEngine;
import com.hulushuju.core.net.http.HttpRequest;
import com.hulushuju.core.net.http.HttpResponse;
import com.hulushuju.core.net.http.callback.HttpCallback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;

/**
 * @Author: Hanjiafeng
 * @Date: 2019-05-29
 * @Desc: 使用OkHttp实现请求发送
 */
public class OkHttpEngineImpl implements HttpEngine {

    private OkHttpClient okHttpClient;

    public OkHttpEngineImpl(OkHttpClient okHttpClient) {
        this.okHttpClient = okHttpClient;
    }

    @Override
    public HttpResponse execute(HttpRequest httpRequest) throws IOException {
        //从HttpRequest中转换OkHttp.Request
        Request request = httpRequest.convertRequest(Request.class);
        //调用请求
        Response response = okHttpClient.newCall(request).execute();
        return new OkHttpResponseImpl(response);
    }

    @Override
    public void asyncExecute(HttpRequest httpRequest, HttpCallback httpCallback) throws IOException {

    }

    @Override
    public void close() {

    }
}
